package com.asurplus.weixin.controller;

import com.asurplus.common.utils.ResponseResult;
import com.asurplus.weixin.service.WeiXinService;
import com.asurplus.weixin.utils.SignUtil;
import com.asurplus.weixin.utils.WeiXinUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lizhou
 * @since 2020-10-13
 **/
@Api(tags = "微信端--微信公众号")
@RestController
@RequestMapping("weixin")
public class WeiXinController {

    @Autowired
    private WeiXinService weiXinService;
    @Autowired
    private WeiXinUtils weiXinUtils;
    @Autowired
    private SignUtil signUtil;

    /**
     * 接口配置信息
     */
    @GetMapping("")
    @ResponseBody
    public String doGet(HttpServletRequest request) {
        // 微信加密签名
        String signature = request.getParameter("signature");
        // 时间戳
        String timestamp = request.getParameter("timestamp");
        // 随机数
        String nonce = request.getParameter("nonce");
        // 随机字符串
        String echostr = request.getParameter("echostr");
        // 请求校验，若校验成功则原样返回echostr，表示接入成功，否则接入失败
        if (signUtil.checkSignature(signature, timestamp, nonce)) {
            return echostr;
        }
        return "";
    }

    /**
     * 接收事件推送
     */
    @PostMapping("")
    @ResponseBody
    public String doPost(HttpServletRequest request) {
        // 微信加密签名
        String signature = request.getParameter("signature");
        // 时间戳
        String timestamp = request.getParameter("timestamp");
        // 随机数
        String nonce = request.getParameter("nonce");
        // 请求校验，若校验成功则原样返回echostr，表示接入成功，否则接入失败
        if (signUtil.checkSignature(signature, timestamp, nonce)) {
            return weiXinService.processRequest(request);
        }
        return "";
    }

    /**
     * JS接口域名认证
     */
    @RequestMapping("MP_verify_IALe3UFyFI8QQZRG.txt")
    @ResponseBody
    public String wxPrivateKey(){
        // 直接返回你下载的授权文件里的内容就好
        return "IALe3UFyFI8QQZRG";
    }

    /**
     * 获取微信服务器IP地址
     */
    @GetMapping("getCallbackIp")
    @ResponseBody
    public ResponseResult getCallbackIp() {
        return ResponseResult.success(weiXinUtils.getCallbackIp());
    }

    /**
     * 创建菜单
     */
    @GetMapping("createMenu")
    @ResponseBody
    public ResponseResult createMenu() {
        return weiXinUtils.createMenu();
    }

    /**
     * 获取 access_token
     */
    @GetMapping("getAccessToken")
    @ResponseBody
    public String getAccessToken() {
        return weiXinUtils.getAccessToken();
    }

    /**
     * 发送文本消息
     */
    @GetMapping("sentTextMessage")
    @ResponseBody
    public ResponseResult sentTextMessage(String openId, String msg) {
        return weiXinUtils.sentTextMessage(openId, msg);
    }

    /**
     * 获取用户信息
     */
    @GetMapping("getUserInfo")
    @ResponseBody
    public ResponseResult getUserInfo(String openId) {
        return weiXinUtils.getUserInfo(openId);
    }
}
